package com.opal.modules.costReportForm.service.impl;

import com.opal.consts.CommonConst;
import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.modules.costReportForm.entity.table.*;
import com.opal.modules.costReportForm.mapper.mysql.CartonCompanyDetailsMapper;
import com.opal.modules.costReportForm.mapper.mysql.CartonDetailsMapper;
import com.opal.modules.costReportForm.mapper.mysql.CartonPriceDetailsMapper;
import com.opal.modules.costReportForm.model.CartonData;
import com.opal.modules.costReportForm.model.CartonPriceDetailsRecord;
import com.opal.modules.costReportForm.service.CartonDetailsService;
import com.opal.utils.UserUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 纸箱sevice层
 */
@Service
public class CartonDetailsServiceImpl  implements CartonDetailsService {

    @Autowired
    private CartonDetailsMapper cartonDetailsMapper;
    @Autowired
    private CartonCompanyDetailsMapper cartonCompanyDetailsMapper;
    @Autowired
    private CartonPriceDetailsMapper cartonPriceDetailsMapper;

    @Override
    public Object onConditiongetCartonDetailsService(CartonData cartonData) {
        //
        List<CartonData> resultData= cartonDetailsMapper.onConditiongetCartonDetails(cartonData);
        return resultData;
    }

    @Override
    public Object getCartonPriceDetailsRecordByTAndcostReportIdService(CartonPriceDetailsRecord cartonPriceDetailsRecord) {
       List<CartonPriceDetailsRecord> result= cartonDetailsMapper.getCartonPriceDetailsRecordByTAndcostReportId(cartonPriceDetailsRecord);
        return result;
    }

    @Override
    public int insertCartonPriceDetailsRecordService(List<CartonPriceDetailsRecord> cartonPriceDetailsRecords, String costReportId) {
        try {
            for (CartonPriceDetailsRecord cartonPriceDetailsRecord : cartonPriceDetailsRecords) {
                cartonPriceDetailsRecord.setCostReportId(costReportId);
                cartonDetailsMapper.insertCartonPriceDetailsRecord(cartonPriceDetailsRecord);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int deleteCartonPriceDetailsRecordService(String costReportId,int i) {
      return  cartonDetailsMapper.deleteCartonPriceDetailsRecord(costReportId,i);
    }

    @Override
    public int deleteCartonPriceDetailsRecordService(String costReportId) {
        return  cartonDetailsMapper.deleteCartonPriceDetailsRecordByCostReportId(costReportId);
    }

    @Override
    public Object getCartonCompanyService() {
        CartonCompanyDetailsExample cartonCompanyDetailsExample = new CartonCompanyDetailsExample();
        String pkOrg="";
        if(UserUtils.getSessionUser().getPkOrg().equals(CommonConst.ZHAOTING)){
            pkOrg="惠州兆婷化妆品有限公司";
        }else {
            pkOrg="澳宝化妆品（惠州）有限公司";
        }
        cartonCompanyDetailsExample.createCriteria().andPkOrgEqualTo(pkOrg);
        List<CartonCompanyDetailsWithBLOBs> cartonCompanyDetailsWithBLOBs = cartonCompanyDetailsMapper.selectByExampleWithBLOBs(cartonCompanyDetailsExample);
        //过滤重复的
        List<CartonCompanyDetailsWithBLOBs> c2=cartonCompanyDetailsWithBLOBs.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(CartonCompanyDetailsWithBLOBs::getCompanyName))), ArrayList::new));
        return c2;
    }

    @Override
    public Object getCartonCompanyAndPriceService(Long id) {
        Map<String,Object> map = new HashedMap();
        //根据id获取公司信息
        CartonCompanyDetailsWithBLOBs cartonCompanyDetailsWithBLOBs = cartonCompanyDetailsMapper.selectByPrimaryKey(id);
        map.put("company",cartonCompanyDetailsWithBLOBs);
        //根据id获取纸箱价格
        CartonPriceDetailsExample cartonPriceDetailsExample =new CartonPriceDetailsExample();
        cartonPriceDetailsExample.createCriteria().andCompanyIdEqualTo(id);
        List<CartonPriceDetails> cartonPriceDetails = cartonPriceDetailsMapper.selectByExample(cartonPriceDetailsExample);
        map.put("cp",cartonPriceDetails);
        return map;
    }

    @Override
    public Object addCartonCompanyInfoService(CartonCompanyDetailsWithBLOBs companyDetailsWithBLOBs) {
        companyDetailsWithBLOBs.setPkOrg(getSessionPkOrgName());
        int i = cartonCompanyDetailsMapper.insertSelective(companyDetailsWithBLOBs);
        if (i>0){
            return new R(WSC.RESTPONSE_OK,"添加成功！");
        }
        return new R(WSC.REQUEST_ERROR,"添加失败：系统维护中请稍后再试");
    }

    @Override
    @Transactional(transactionManager = "mysqlTransactionManager")
    public Object deleteCartonCompanyInfoService(Long id) {
        try {
            //先删除当前公司的数据
            CartonPriceDetailsExample cartonPriceDetailsExample= new CartonPriceDetailsExample();
            cartonPriceDetailsExample.createCriteria().andCompanyIdEqualTo(id);
            cartonPriceDetailsMapper.deleteByExample(cartonPriceDetailsExample);
            //然后删除公司的数据
            cartonCompanyDetailsMapper.deleteByPrimaryKey(id);
            return new R(WSC.RESTPONSE_OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new R(WSC.REQUEST_ERROR,"删除失败:系统维护中请稍后再试！");
        }

    }

    @Override
    public Object updateCartonCompanyInfoService(CartonCompanyDetailsWithBLOBs companyDetailsWithBLOBs) {
        int i = cartonCompanyDetailsMapper.updateByPrimaryKeySelective(companyDetailsWithBLOBs);
        if (i>0){
            return new R(WSC.RESTPONSE_OK,"修改成功！");
        }
        return new R(WSC.REQUEST_ERROR,"修改失败:系统维护中请稍后再试");
    }

    @Override
    public Object saveCartonPriceService(CartonPriceDetails cartonPriceDetails) {
        int i = cartonPriceDetailsMapper.insertSelective(cartonPriceDetails);
        if (i>0){
            return  new R(WSC.RESTPONSE_OK,"保存成功");
        }else {
            return new R(WSC.REQUEST_ERROR,"保存失败，请稍后重试！");
        }
    }

    @Override
    public Object updateCartonPriceService(CartonPriceDetails cartonPriceDetails) {
        int i = cartonPriceDetailsMapper.updateByPrimaryKey(cartonPriceDetails);
        if (i>0){
            return  new R(WSC.RESTPONSE_OK,"修改成功");
        }else {
            return new R(WSC.REQUEST_ERROR,"修改失败，请稍后重试！");
        }
    }

    @Override
    public Object deleteCartonPriceService(Long id) {
        int i = cartonPriceDetailsMapper.deleteByPrimaryKey(id);
        if (i>0){
            return  new R(WSC.RESTPONSE_OK,"删除成功");
        }else {
            return new R(WSC.REQUEST_ERROR,"删除失败，请稍后重试！");
        }
    }

    @Override
    public Object getCartonCompanyCompanyNameService(String companyName) {
        CartonCompanyDetailsExample cartonCompanyDetailsExample = new CartonCompanyDetailsExample();
        cartonCompanyDetailsExample.createCriteria().andCompanyNameEqualTo(companyName).andPkOrgEqualTo(getSessionPkOrgName());
        List<CartonCompanyDetailsWithBLOBs> cartonCompanyDetails = cartonCompanyDetailsMapper.selectByExampleWithBLOBs(cartonCompanyDetailsExample);
        return cartonCompanyDetails;
    }

    @Override
    public Object getCartonCompanyByNameAndCodeService(String companyName, String quotationCode) {
        CartonCompanyDetailsExample cartonCompanyDetailsExample = new CartonCompanyDetailsExample();
        cartonCompanyDetailsExample.createCriteria().andCompanyNameEqualTo(companyName).andPkOrgEqualTo(getSessionPkOrgName()).andQuotationCodeEqualTo(quotationCode);
        List<CartonCompanyDetailsWithBLOBs> cartonCompanyDetails = cartonCompanyDetailsMapper.selectByExampleWithBLOBs(cartonCompanyDetailsExample);
        return cartonCompanyDetails;
    }

    private String getSessionPkOrgName(){
        String pkOrg="";
        if(UserUtils.getSessionUser().getPkOrg().equals(CommonConst.AOBAO)){
            pkOrg="澳宝化妆品（惠州）有限公司";
        }else {
            pkOrg="惠州兆婷化妆品有限公司";
        }
        return pkOrg;
    }
}
